#import libraries
library(psych)
library(MASS)
library(quantreg)
library(pwr)
library(dplyr)


#load data
data <- read.csv("Study 1.csv")

View(data)


#FOCAL ANALYSES (dv:Log_AveInvestment)
##the robust linear regression
robust_model <- rlm(Log_AveInvestment ~ Gini*ProsperScore + Income + PovertyRate + Population + Gini*EstimatedReturn + Log_LoanDuration + ListingMonth + BorrowerState, data = data, psi = psi.huber)
summary(robust_model)

##the quantile regression
qr_model <- rq(Log_AveInvestment ~ Gini*ProsperScore + Income + PovertyRate + Population + Gini*EstimatedReturn +  Log_LoanDuration + ListingMonth + BorrowerState, data = data, tau = 0.5)
summary(qr_model)

##the robust linear regression excluding the extreme outliers
q_low  <- quantile(data$Log_AveInvestment, 0.01, na.rm = TRUE)
q_high <- quantile(data$Log_AveInvestment, 0.99, na.rm = TRUE)

data_drop_outlier <- data %>%
  filter(Log_AveInvestment > q_low,
         Log_AveInvestment < q_high)


robust_model_drop_outlier_sup <- rlm(Log_AveInvestment ~ Gini*ProsperScore + Income + PovertyRate + Population + Gini*EstimatedReturn + Log_LoanDuration + ListingMonth + BorrowerState + LoanOriginalAmount, data = data_drop_outlier, psi = psi.huber)
summary(robust_model_drop_outlier)


#POST-TEST and people's perception of economic inequality 
##load post-test data
data_post <- read.csv("Study 1 - post test.csv")
View(data_post)

##descriptive
mean(data_post$correct_rate) #M = .68
sd(data_post$correct_rate) #SD = .21
median(data_post$correct_rate) #median = .7

##one-sample t-test
t.test(data_post$correct_rate, mu = .5) #t(98) = 8.41, p < .001

##wilcoxon signed-Rank test
wilcox.test(data_post$correct_rate, mu = 0.5, alternative = "two.sided") #V = 3655, p < .001

##achieved power for t-test
d <- (mean(data_post$correct_rate) - .5) / sd(data_post$correct_rate) 
pwr.t.test(n = 99, d = d, sig.level = 0.05, type = "one.sample", alternative = "two.sided") # achieved power = 1